package com.example.musicbox.service.impl;

import com.example.musicbox.config.MyException;
import com.example.musicbox.config.ThreadPoolEx;
import com.example.musicbox.entity.MLog;
import com.example.musicbox.entity.dto.LogDto;
import com.example.musicbox.enums.AboutMusic;
import com.example.musicbox.enums.LogDoType;
import com.example.musicbox.enums.MusicType;
import com.example.musicbox.mapper.MLogMapper;
import com.example.musicbox.mapper.MUserMapper;
import com.example.musicbox.service.MLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.logging.Logger;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅哥是我
 * @since 2022-08-01
 */
@Service
public class MLogServiceImpl extends ServiceImpl<MLogMapper, MLog> implements MLogService {


    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private MUserMapper mapper;

    @Resource
    private MLogMapper mLogMapper;


    /**
     * 日志信息插入
     * @param dowhat
     * @param aboutMusic
     * @param mId
     * @param logDoType
     * @param token
     */
    @Async("taskExecutor")
    @Override
    public void insertLog(String dowhat, AboutMusic aboutMusic, Integer mId, LogDoType logDoType, String token) {
        try {
            Integer uId = (Integer) redisTemplate.opsForValue().get(token);
            String username = mapper.findUsername(uId);
            MLog mLog = MLog.builder()
                    .doType(String.valueOf(aboutMusic.getCode()))
                    .dowhat(dowhat)
                    .lType(String.valueOf(logDoType.getCode()))
                    .uid(uId)
                    .uname(username)
                    .mId(mId)
                    .build();
            mLogMapper.insert(mLog);
        }catch (Exception e){
            throw new MyException("信息录入失败");
        }
    }
}
